#---------------------------------------------------------
# Are Intermediary Constraints Priced?
# Du, Hébert, Huber
# The Review of Financial Studies 2022
#---------------------------------------------------------

#---------------------------------------------------------
# (0) Clean Bloomberg downloads to usable format
# (1) Impute missing IBOR tenors (< 1Y)
# (2) Convert forward points to rates, all in FX/USD
# (3) Merge all rates to one dataframe per currency
# (4) Remove CHF OIS > 2017; bad data in FRA
# (5) Clean Bloomberg settlement dates data
#---------------------------------------------------------

#---------------------------------------------------------
# Set-up
#---------------------------------------------------------

if (exists('script_path')) {
  rm(list = setdiff(ls(), 'script_path'))
} else {
  args = commandArgs(trailingOnly = T)
  if (length(args) > 0) {
    script_path <- args[1]  
    rm(list = setdiff(ls(), 'script_path'))
  } else {
    rm(list = ls())
    script_path <- '~/Dropbox/ForwardArbitrage/CodeReplication/'
  } 
}

source(paste0(script_path, 'RCode/ArbFunc.R'))
currency_names <- generate_currency_names(number_of_currencies = 6) #c('AUD', 'CAD', 'GBP', 'EUR', 'CHF', 'JPY'): decreasing order of 1M-f 3M returns

#---------------------------------------------------------
# Construct OIS data of maturity up to 12M
#---------------------------------------------------------

# Initiate objects to hold data and temp modification.
orig_ois_names <- c(currency_names, 'USD', 'CHF_SFSNT', 'CHF_SFSO')
ois_names <- sapply(c(currency_names, 'USD', 'CHF_SFSNT', 'CHF_SFSO'), function(x) paste('ois', x, sep = '_'))

for (l in 1:length(orig_ois_names)) {
  assign(paste('orig', orig_ois_names[l], sep = '_'), read_excel(paste0(script_path, 'DataSkeleton/OISData_06082021.xlsx'), sheet = orig_ois_names[l]))
}

list_temp <- vector('list', 1)

rate_names <- c('index', 'one_wk', 'two_wk', 'three_wk', 'one_mth', 'two_mth', 'three_mth', 'four_mth', 'five_mth', 'six_mth',
                'seven_mth', 'eight_mth', 'nine_mth', 'ten_mth', 'eleven_mth', 'one_yr') #NOTE: using 'one_yr' here as there is no 1Y+

# Assemble for each currency rates in one table.
for (l in 1:length(orig_ois_names)){
  orig_temp <- get(paste('orig', orig_ois_names[l], sep = '_'))
  orig_temp <- orig_temp[-(1:2), -(1:5)]
  empty_cols <- c(1:dim(orig_temp)[2]) %% 3 == 0
  orig_temp <- orig_temp[, !empty_cols]
  
  # combine all columns of rates by dates
  colnames(orig_temp)[c(1:dim(orig_temp)[2])[c(1:dim(orig_temp)[2]) %% 2 == 0]] <- rate_names
  
  # first separate into individual dataframe and remove NAs
  for (i in 1:length(rate_names)){
    list_temp[[i]] <- as.data.frame(orig_temp[, (2 * (i - 1) + 1):(2 * i)])
    list_temp[[i]][, 1] <- as.Date(as.numeric(list_temp[[i]][, 1]), origin = excel_origin)
    list_temp[[i]][, 2] <- as.numeric(list_temp[[i]][, 2])
    colnames(list_temp[[i]])[1] <- c('Date')
    list_temp[[i]] <- na.omit(list_temp[[i]])
  }
  
  # merge all dataframe into one by Date 
  temp_ois <- Reduce(function(x, y) {merge(x, y, all = TRUE)}, list_temp)
  temp_ois <- temp_ois[order(temp_ois$Date), ]
  assign(ois_names[l], data.table(temp_ois))
}

#---------------------------------------------------------
# Construct forward data of maturity up to 12M
#---------------------------------------------------------

# Initiate objects to hold data and temp modification.
orig_fwd_names <- sapply(currency_names, function(x) paste('orig', x, sep = '_'))
fwd_names <- sapply(currency_names, function(x) paste('fwd', x, sep = '_'))

for (l in 1:length(orig_fwd_names)) {
  assign(orig_fwd_names[l], read_excel(paste0(script_path, 'DataSkeleton/ForwardData_06082021.xlsx'), sheet = currency_names[l]))
}

list_temp <- vector('list', 1)

rate_names <- c('ON', 'TN', 'spot', 'SN', 'one_wk', 'two_wk', 'three_wk', 'one_mth', 'two_mth', 'three_mth', 'four_mth', 
                'five_mth', 'six_mth', 'seven_mth', 'eight_mth', 'nine_mth', 'ten_mth', 'eleven_mth', 'twelve_mth')

# Assemble for each currency rates in one table.
for (l in 1:length(orig_fwd_names)){
  orig_temp <- get(orig_fwd_names[l])
  orig_temp <- orig_temp[-(1:2),-(1:3)]
  empty_cols <- c(1:dim(orig_temp)[2]) %% 3 == 0
  orig_temp <- orig_temp[, !empty_cols]
  
  # combine all columns of rates by dates
  colnames(orig_temp)[c(1:dim(orig_temp)[2])[c(1:dim(orig_temp)[2]) %% 2 == 0]] <- rate_names
  
  # first separate into individual dataframe and remove NAs
  for (i in 1:length(rate_names)){
    list_temp[[i]] <- as.data.frame(orig_temp[, (2 * (i - 1) + 1):(2 * i)])
    list_temp[[i]][, 1] <- as.Date(as.numeric(list_temp[[i]][, 1]), origin = excel_origin)
    list_temp[[i]][, 2] <- as.numeric(list_temp[[i]][, 2])
    colnames(list_temp[[i]])[1] <- c('Date')
    list_temp[[i]] <- na.omit(list_temp[[i]])
  }
  
  # merge all dataframe into one by Date 
  temp_fwd <- Reduce(function(x, y) {merge(x, y, all = TRUE)}, list_temp)
  temp_fwd <- temp_fwd[order(temp_fwd$Date), ]
  # temp_fwd <- temp_fwd[temp_fwd$Date > start_date & temp_fwd$Date < end_date, ]
  assign(fwd_names[l], temp_fwd)
}

corrected <- correct_outliers(missing_or_correct = 'missing', fwd_AUD, fwd_CAD, fwd_CHF, ois_AUD, ois_CHF)
list2env(corrected, .GlobalEnv)

## Check that order is the same as CURRENCY_NAMES
list_ois <- list(data.table(ois_AUD), data.table(ois_CAD), data.table(ois_GBP), data.table(ois_EUR), 
                 data.table(ois_CHF), data.table(ois_JPY), data.table(ois_USD))
names(list_ois) <- c(currency_names, 'USD')

list_fwd <- list(data.table(fwd_AUD), data.table(fwd_CAD), data.table(fwd_GBP), 
                 data.table(fwd_EUR), data.table(fwd_CHF), data.table(fwd_JPY))
names(list_fwd) <- currency_names

#---------------------------------------------------------
# Construct unsecured rates (IBOR only)
#---------------------------------------------------------

unsecured <- data.table(read_excel(paste0(script_path, 'DataSkeleton/Bloomberg_Unsecured_Tickers_06082021.xlsx'), sheet = 'IBOR_value', 
                                   col_types = c('date', rep('numeric', 87))))
names(unsecured) <- substr(names(unsecured), 0, ifelse(regexpr(' ', names(unsecured)) < 0, 99, regexpr(' ', names(unsecured)) - 1))
unsecured$Date <- ymd(unsecured$Date)

#---------------------------------------------------------
# IBOR by currency

# Currencies requiring no adjustment: CAD (CDS), NZD (NDS), SEK (SKS), USD (USS)
ibor_AUD <- unsecured[, c('Date', names(unsecured)[substr(names(unsecured), 0, 4) == 'ADBB']), with = FALSE]
ibor_CAD <- unsecured[, c('Date', names(unsecured)[substr(names(unsecured), 0, 4) == 'CDOR']), with = FALSE]
ibor_CHF <- unsecured[, c('Date', names(unsecured)[substr(names(unsecured), 0, 4) == 'SF00' &
                                                     substr(names(unsecured), nchar(names(unsecured)), nchar(names(unsecured))) == 'M']), with = FALSE]
ibor_DKK <- unsecured[, c('Date', names(unsecured)[substr(names(unsecured), 0, 4) == 'CIBO' &
                                                     substr(names(unsecured), nchar(names(unsecured)), nchar(names(unsecured))) == 'M']), with = FALSE]
ibor_EUR <- unsecured[, c('Date', names(unsecured)[substr(names(unsecured), 0, 4) == 'EUR0' &
                                                     substr(names(unsecured), nchar(names(unsecured)), nchar(names(unsecured))) == 'M']), with = FALSE]
ibor_GBP <- unsecured[, c('Date', names(unsecured)[substr(names(unsecured), 0, 4) == 'BP00' &
                                                     substr(names(unsecured), nchar(names(unsecured)), nchar(names(unsecured))) == 'M']), with = FALSE]
ibor_JPY <- unsecured[, c('Date', names(unsecured)[substr(names(unsecured), 0, 4) == 'JY00' &
                                                     substr(names(unsecured), nchar(names(unsecured)), nchar(names(unsecured))) == 'M']), with = FALSE]
ibor_NOK <- unsecured[, c('Date', names(unsecured)[substr(names(unsecured), 0, 4) == 'NIBO' &
                                                     substr(names(unsecured), nchar(names(unsecured)), nchar(names(unsecured))) == 'M']), with = FALSE]
ibor_NZD <- unsecured[, c('Date', names(unsecured)[substr(names(unsecured), 0, 4) == 'NDBB' &
                                                     substr(names(unsecured), nchar(names(unsecured)), nchar(names(unsecured))) == 'M']), with = FALSE]
ibor_SEK <- unsecured[, c('Date', names(unsecured)[substr(names(unsecured), 0, 4) == 'STIB' &
                                                     substr(names(unsecured), nchar(names(unsecured)), nchar(names(unsecured))) == 'M']), with = FALSE]
ibor_USD <- unsecured[, c('Date', names(unsecured)[substr(names(unsecured), 0, 4) == 'US00' &
                                                     substr(names(unsecured), nchar(names(unsecured)), nchar(names(unsecured))) == 'M']), with = FALSE]

names(ibor_AUD)[-1] <- c('one_mth', 'two_mth', 'three_mth', 'four_mth', 'five_mth', 'six_mth', 'nine_mth', 'one_yr')
names(ibor_CAD)[-1] <- c('one_mth', 'two_mth', 'three_mth', 'six_mth', 'one_yr')
names(ibor_CHF)[-1] <- c('one_mth', 'two_mth', 'three_mth', 'six_mth', 'one_yr')
names(ibor_DKK)[-1] <- c('one_mth', 'two_mth', 'three_mth', 'six_mth', 'one_yr')
names(ibor_EUR)[-1] <- c('one_mth', 'two_mth', 'three_mth', 'six_mth', 'nine_mth', 'one_yr')
names(ibor_GBP)[-1] <- c('one_mth', 'two_mth', 'three_mth', 'six_mth', 'one_yr')
names(ibor_JPY)[-1] <- c('one_mth', 'two_mth', 'three_mth', 'six_mth', 'one_yr')
names(ibor_NOK)[-1] <- c('one_mth', 'two_mth', 'three_mth', 'six_mth', 'nine_mth')
names(ibor_NZD)[-1] <- c('one_mth', 'two_mth', 'three_mth', 'four_mth', 'five_mth', 'six_mth', 'nine_mth', 'one_yr')
names(ibor_SEK)[-1] <- c('one_mth', 'two_mth', 'three_mth', 'six_mth')
names(ibor_USD)[-1] <- c('one_mth', 'two_mth', 'three_mth', 'six_mth', 'one_yr')

list_ibor <- list(ibor_AUD, ibor_CAD, ibor_GBP, ibor_EUR, ibor_CHF, ibor_JPY, ibor_USD)
names(list_ibor) <- c(currency_names, 'USD')

#---------------------------------------------------------
# Construct FRA rates
#---------------------------------------------------------

fra <- data.table(read_excel(paste0(script_path, 'DataSkeleton/OISData_06082021.xlsx'), sheet = 'FRA'))
fra_names <- c(apply(expand.grid(c('ibor_1M_fwd_1M', 'ibor_1M_fwd_3M', 'ibor_3M_fwd_3M'), c('USD', 'EUR')), 1,
                             function(tuple) paste(tuple[2], tuple[1], sep = '_')),
               apply(expand.grid(c('ibor_1M_fwd_3M', 'ibor_3M_fwd_3M'), c('JPY', 'GBP', 'CHF', 'CAD', 'AUD', 'NZD')), 1,
                     function(tuple) paste(tuple[2], tuple[1], sep = '_')))

fra <- fra[-(1:2),]
empty_cols <- c(1:dim(fra)[2]) %% 3 == 0
fra <- fra[, !empty_cols, with = FALSE]
colnames(fra)[c(1:dim(fra)[2])[c(1:dim(fra)[2]) %% 2 == 0]] <- fra_names

# first separate into individual dataframe and remove NAs
list_temp <- vector('list', 1)
for (i in 1:length(fra_names)){
  list_temp[[i]] <- as.data.frame(fra[, (2 * (i - 1) + 1):(2 * i)])
  list_temp[[i]][, 1] <- as.Date(as.numeric(list_temp[[i]][, 1]), origin = excel_origin)
  list_temp[[i]][, 2] <- as.numeric(list_temp[[i]][, 2])
  colnames(list_temp[[i]])[1] <- c('Date')
  list_temp[[i]] <- na.omit(list_temp[[i]])
}

# merge all dataframe into one by Date 
fra <- Reduce(function(x, y) {merge(x, y, all = TRUE)}, list_temp)
fra <- data.table(fra[order(fra$Date), ])

#---------------------------------------------------------
# Impute IBOR for every month within a year
#---------------------------------------------------------

# Currencies that require 4M, 5M
for (c in c('CAD', 'CHF', 'EUR', 'GBP', 'JPY', 'USD')) {
  list_ibor[[c]][, four_mth := linear_imputation(start_level = three_mth, end_level = six_mth, period_in_between = 3, period_to_impute = 1)]
  list_ibor[[c]][, five_mth := linear_imputation(start_level = three_mth, end_level = six_mth, period_in_between = 3, period_to_impute = 2)]
}

# Currencies that require 7M, 8M, 9M, 10M, 11M
for (c in c('CAD', 'CHF', 'GBP', 'JPY', 'USD')) {
  list_ibor[[c]][, seven_mth := linear_imputation(start_level = six_mth, end_level = one_yr, period_in_between = 6, period_to_impute = 1)]
  list_ibor[[c]][, eight_mth := linear_imputation(start_level = six_mth, end_level = one_yr, period_in_between = 6, period_to_impute = 2)]
  list_ibor[[c]][, nine_mth := linear_imputation(start_level = six_mth, end_level = one_yr, period_in_between = 6, period_to_impute = 3)]
  list_ibor[[c]][, ten_mth := linear_imputation(start_level = six_mth, end_level = one_yr, period_in_between = 6, period_to_impute = 4)]
  list_ibor[[c]][, eleven_mth := linear_imputation(start_level = six_mth, end_level = one_yr, period_in_between = 6, period_to_impute = 5)]
}

# Currencies that require 7M, 8M, 10M, 11M, but NOT 9M
for (c in c('AUD', 'EUR')) {
  list_ibor[[c]][, seven_mth := linear_imputation(start_level = six_mth, end_level = nine_mth, period_in_between = 3, period_to_impute = 1)]
  list_ibor[[c]][, eight_mth := linear_imputation(start_level = six_mth, end_level = nine_mth, period_in_between = 3, period_to_impute = 2)]
  list_ibor[[c]][, ten_mth := linear_imputation(start_level = nine_mth, end_level = one_yr, period_in_between = 3, period_to_impute = 1)]
  list_ibor[[c]][, eleven_mth := linear_imputation(start_level = nine_mth, end_level = one_yr, period_in_between = 3, period_to_impute = 2)]
}

for (c in 1:length(list_ibor)) {
  setcolorder(list_ibor[[c]], c('Date', tenor_lookup$tenor_name[2:13]))
}

#---------------------------------------------------------
# Convert forward points to FX rates
# base_currency and fwd_factor are looked up under CURRENCY_ois
#---------------------------------------------------------

list_fx <- list()
short_fwd_names <- c(tenor_lookup$tenor_name[2:12], 'twelve_mth')

for (l in 1:length(list_fwd)) {
  base_currency <- currency_convention$base_currency[rownames(currency_convention) == paste(names(list_fwd)[l], 'ois', sep = '_')]
  fwd_factor <- currency_convention$fwd_factor[rownames(currency_convention) == paste(names(list_fwd)[l], 'ois', sep = '_')]
  
  temp <- list_fwd[[l]]
  temp <- temp[, c('Date', 'spot', short_fwd_names), with = FALSE]
  names(temp)[which(names(temp) == 'spot')] <- 'quoted_spot'
  names(temp)[names(temp) %in% short_fwd_names] <- paste('fwd_pt', short_fwd_names, sep = '_')
  
  if (base_currency != 'USD') {
    temp[, spot := 1 / quoted_spot]
    temp[, (short_fwd_names) := lapply(.SD, function(x) 1 / (quoted_spot + x / fwd_factor)), .SDcols = paste('fwd_pt', short_fwd_names, sep = '_')]
  } else {
    temp[, spot := copy(quoted_spot)]
    temp[, (short_fwd_names) := lapply(.SD, function(x) quoted_spot + x / fwd_factor), .SDcols = paste('fwd_pt', short_fwd_names, sep = '_')]
  }
  
  list_fx[[l]] <- temp[, c('Date', 'spot', short_fwd_names), with = FALSE]
  names(list_fx[[l]])[which(names(list_fx[[l]]) == 'twelve_mth')] <- 'one_yr'
}
names(list_fx) <- names(list_fwd)

#---------------------------------------------------------
# Delete CHF OIS data post 2017 Dec 31 due to reference change
#---------------------------------------------------------

list_ois[['CHF']] <- list_ois[['CHF']][Date <= ymd('2017-12-31'), ]

#---------------------------------------------------------
# Remove FRA / OIS data that are suspicious
#---------------------------------------------------------

fra[AUD_ibor_1M_fwd_3M > 6.8 & Date < start_crisis_date, AUD_ibor_1M_fwd_3M := NA]
fra[Date >= ymd('2004-07-16') & Date <= ymd('2005-04-07'), AUD_ibor_1M_fwd_3M := NA]

fra[Date >= ymd('2005-04-08') & Date <= ymd('2005-09-23'), CAD_ibor_1M_fwd_3M := NA]

fra[JPY_ibor_1M_fwd_3M > 1.2 & Date > start_crisis_date, JPY_ibor_1M_fwd_3M := NA]
fra[Date >= ymd('2006-06-28') & Date <= ymd('2006-08-29'), JPY_ibor_1M_fwd_3M := NA]
fra[JPY_ibor_3M_fwd_3M > 10 & Date < start_crisis_date, JPY_ibor_3M_fwd_3M := NA]

fra[Date >= ymd('2005-05-18') & Date <= ymd('2006-10-20'), NZD_ibor_1M_fwd_3M := NA]
fra[Date >= ymd('2005-06-28') & Date <= ymd('2006-10-20'), NZD_ibor_3M_fwd_3M := NA]

list_ois[['CAD']][Date >= ymd('2003-05-01') & Date <= ymd('2003-10-24'), four_mth := NA]
list_ois[['CAD']][Date >= ymd('2003-05-01') & Date <= ymd('2003-10-24'), six_mth := NA]

#---------------------------------------------------------
# Merge all relevant rates for one currency into one
#---------------------------------------------------------

list_rates <- list()

# OIS, IBOR, FX
for (c in 1:length(currency_names)) {
  temp_ois <- list_ois[[currency_names[c]]]
  name_ois <- c('index', names(temp_ois)[names(temp_ois) %in% tenor_lookup$tenor_name])
  short_name_ois <- c('index', tenor_lookup$tenor_short_name[match(name_ois[-1], tenor_lookup$tenor_name)])
  
  temp_ibor <- list_ibor[[currency_names[c]]]
  name_ibor <- names(temp_ibor)[names(temp_ibor) %in% tenor_lookup$tenor_name]
  short_name_ibor <- tenor_lookup$tenor_short_name[match(name_ibor, tenor_lookup$tenor_name)]
  
  temp_fx <- list_fx[[currency_names[c]]]
  name_fx <- names(temp_fx)[names(temp_fx) %in% tenor_lookup$tenor_name]
  short_name_fx <- tenor_lookup$tenor_short_name[match(name_fx, tenor_lookup$tenor_name)]
  
  list_rates[[c]] <- multiple_reduced_join(merge_key = 'Date', merge_all_x = TRUE, merge_all_y = TRUE,
                                           data_list = list(temp_ois[, c('Date', name_ois), with = FALSE],
                                                            temp_ibor[, c('Date', name_ibor), with = FALSE],
                                                            temp_fx[, c('Date', name_fx), with = FALSE]))  
  names(list_rates[[c]]) <- c('Date', paste(currency_names[c], 'ois', short_name_ois, sep = '_'),
                              paste(currency_names[c], 'ibor', short_name_ibor, sep = '_'),
                              paste(currency_names[c], 'fx', short_name_fx, sep = '_'))
}

# USD, which only has OIS and IBOR
temp_ois <- list_ois[['USD']]
name_ois <- c('index', names(temp_ois)[names(temp_ois) %in% tenor_lookup$tenor_name])
short_name_ois <- c('index', tenor_lookup$tenor_short_name[match(name_ois[-1], tenor_lookup$tenor_name)])
temp_ibor <- list_ibor[['USD']]
name_ibor <- names(temp_ibor)[names(temp_ibor) %in% tenor_lookup$tenor_name]
short_name_ibor <- tenor_lookup$tenor_short_name[match(name_ibor, tenor_lookup$tenor_name)]

list_rates[[length(currency_names) + 1]] <- multiple_reduced_join(merge_key = 'Date', merge_all_x = TRUE, merge_all_y = TRUE,
                                                                  data_list = list(temp_ois[, c('Date', name_ois), with = FALSE],
                                                                                   temp_ibor[, c('Date', name_ibor), with = FALSE]))
names(list_rates[[length(currency_names) + 1]]) <- c('Date', paste('USD_ois', short_name_ois, sep = '_'),
                                                     paste('USD_ibor', short_name_ibor, sep = '_'))

# All currencies that have FRA
list_rates[[length(currency_names) + 2]] <- fra

all_rates <- Reduce(function(x, y) merge(x, y, all = T, by = 'Date'), list_rates)

save(all_rates, file = paste0(script_path, 'OutputInterim/All_Rates.RData'))

#-----------------------------------------------
# Construct settlement days
#-----------------------------------------------
# (1) CSV downloads of daily settlement for most of the regular tenors
# (2) Stata data for daily spot, 1W, 1M, 3M forwards
# (3) CSV downloads of selected dates of quarter-end specials

date_names <- sapply(currency_names, function(x) paste('date', x, sep = '_'))

# Read in CSV dates
for (c in 1:length(currency_names)) {
  temp <- data.table(read_excel(paste0(script_path, 'DataSkeleton/SettlementDates/Settlement_Dates_', currency_names[c], '.xlsx'),
                                sheet = currency_names[c]))
  temp[, `:=` (SETTLE_DT = NULL, `Trade Date Reformatted` = NULL, `...5` = NULL)] #empty column after Trade Date Reformatted renamed '...5'
  temp <- temp[, -1]
  temp$Trade <- ymd(temp$Trade)
  for (var in names(temp)[-1]) {
    temp[, (var) := mdy(get(var))]
  }
  names(temp) <- substr(names(temp), 0, ifelse(regexpr(' ', names(temp)) < 0, 99, regexpr(' ', names(temp)) - 1))
  assign(date_names[c], temp)
}

# Combine Stata dates
# NOTE: Stata file does NOT have AUD1M and AUD3M past 2018.  Need to re-download and merge
stata_dates <- data.table(read_dta(paste0(script_path, 'DataSkeleton/SettlementDates/fx_settlement_dates.dta')))
names(stata_dates)[1] <- 'Trade'
names(stata_dates) <- substr(names(stata_dates), 0, ifelse(regexpr('_', names(stata_dates)) < 0, 99, regexpr('_', names(stata_dates)) - 1))

AUD_supp <- data.table(read_excel(paste0(script_path, 'DataSkeleton/SettlementDates/Settlement_Dates_AUD.xlsx'), sheet = 'AUD_extra'))
AUD_supp[, `:=` (AUD = NULL, SETTLE_DT = NULL, `Trade Date Reformatted` = NULL, `...5` = NULL)]
names(AUD_supp) <- substr(names(AUD_supp), 0, ifelse(regexpr(' ', names(AUD_supp)) < 0, 99, regexpr(' ', names(AUD_supp)) - 1))
AUD_supp[, `:=` (Trade = ymd(Trade), AUD1M = mdy(AUD1M), AUD3M = mdy(AUD3M))]
stata_dates$AUD1M <- AUD_supp$AUD1M
stata_dates$AUD3M <- AUD_supp$AUD3M

for (c in 1:length(date_names)) {
  temp <- get(date_names[c])
  temp <- cbind(temp, stata_dates[, grep(currency_names[c], names(stata_dates)), with = FALSE])
  setcolorder(temp, c(1, order(names(temp)[-1]) + 1))
  names(temp) <- c('Date', 'spot', 'ten_mth', 'eleven_mth', 'one_yr', 'one_mth', 'one_wk', 'two_mth', 'two_wk', 'three_mth', 'three_wk',
                   'four_mth', 'five_mth', 'six_mth', 'seven_mth', 'eight_mth', 'nine_mth', 'ON', 'SN', 'TN')
  setcolorder(temp, c('Date', tenor_lookup$tenor_name[1:13]))
  assign(date_names[c], temp)
}

list_settlement <- list(date_AUD, date_CAD, date_GBP, date_EUR, date_CHF, date_JPY)
names(list_settlement) <- currency_names
save(list_settlement, file = paste0(script_path, 'OutputInterim/Settlement_AllDays.RData'))
